package ynu.edu.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ynu.edu.dto.MerchantDTO;
import ynu.edu.service.MerchantService;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 商家控制器
 */
@RestController
@RequestMapping("/merchant")
@RequiredArgsConstructor
@Slf4j
public class MerchantController {
    
    private final MerchantService merchantService;
    
    /**
     * 获取所有商家
     *
     * @return 商家列表
     */
    @GetMapping("/all")
    public List<MerchantDTO> getAllMerchants() {
        log.info("获取所有商家列表");
        List<MerchantDTO> merchants = merchantService.getAllMerchants();
        
        // 记录详细日志
        log.info("获取到{}个商家", merchants.size());
        if (!merchants.isEmpty()) {
            log.info("第一个商家: id={}, name={}", 
                merchants.get(0).getBusinessId(),
                merchants.get(0).getBusinessName());
        } else {
            log.warn("商家列表为空");
        }
        
        return merchants;
    }
    
    /**
     * 根据ID获取商家
     *
     * @param businessId 商家ID
     * @return 商家信息
     */
    @GetMapping("/getById/{businessId}")
    public ResponseEntity<MerchantDTO> getMerchantById(@PathVariable("businessId") Integer businessId) {
        log.info("根据ID获取商家，businessId: {}", businessId);
        MerchantDTO merchant = merchantService.getMerchantById(businessId);
        log.info("获取到商家: {}", merchant.getBusinessName());
        return ResponseEntity.ok(merchant);
    }
    
    /**
     * 根据点餐分类获取商家列表
     *
     * @param orderTypeId 点餐分类ID
     * @return 商家列表
     */
    @GetMapping("/getByOrderType/{orderTypeId}")
    public List<MerchantDTO> getMerchantsByOrderType(@PathVariable("orderTypeId") Integer orderTypeId) {
        log.info("根据点餐分类获取商家列表，orderTypeId: {}", orderTypeId);
        List<MerchantDTO> merchants = merchantService.getMerchantsByOrderType(orderTypeId);
        log.info("获取到{}个商家", merchants.size());
        return merchants;
    }
    
    /**
     * 添加商家
     *
     * @param merchantDTO 商家信息
     * @return 添加后的商家信息
     */
    @PostMapping("/add")
    public ResponseEntity<MerchantDTO> addMerchant(@Valid @RequestBody MerchantDTO merchantDTO) {
        log.info("添加商家: {}", merchantDTO.getBusinessName());
        MerchantDTO addedMerchant = merchantService.addMerchant(merchantDTO);
        log.info("商家添加成功，ID: {}", addedMerchant.getBusinessId());
        return new ResponseEntity<>(addedMerchant, HttpStatus.CREATED);
    }
    
    /**
     * 更新商家
     *
     * @param merchantDTO 商家信息
     * @return 更新后的商家信息
     */
    @PutMapping("/update")
    public ResponseEntity<MerchantDTO> updateMerchant(@Valid @RequestBody MerchantDTO merchantDTO) {
        log.info("更新商家，businessId: {}, 商家名: {}", merchantDTO.getBusinessId(), merchantDTO.getBusinessName());
        MerchantDTO updatedMerchant = merchantService.updateMerchant(merchantDTO);
        log.info("商家更新成功");
        return ResponseEntity.ok(updatedMerchant);
    }
    
    /**
     * 删除商家
     *
     * @param businessId 商家ID
     * @return 操作结果
     */
    @DeleteMapping("/delete/{businessId}")
    public ResponseEntity<Void> deleteMerchant(@PathVariable("businessId") Integer businessId) {
        log.info("删除商家，businessId: {}", businessId);
        merchantService.deleteMerchant(businessId);
        log.info("商家删除成功");
        return ResponseEntity.noContent().build();
    }
    
    /**
     * 搜索商家
     *
     * @param keyword 关键词
     * @return 商家列表
     */
    @GetMapping("/search")
    public ResponseEntity<List<MerchantDTO>> searchMerchants(@RequestParam String keyword) {
        log.info("搜索商家，关键词: {}", keyword);
        List<MerchantDTO> merchants = merchantService.searchMerchants(keyword);
        log.info("搜索到{}个商家", merchants.size());
        return ResponseEntity.ok(merchants);
    }
} 